<?php
//simple script to install the database

function em_install() {
	global $table_prefix, $wpdb, $em_event_table, $em_registration_table,
			$em_question_table, $em_answer_table;

	require_once(ABSPATH . 'wp-admin/upgrade-functions.php');

		$t1 = "CREATE TABLE `".$em_event_table."` (
			`id` int(11) NOT NULL auto_increment,
			`manager_id` int(11) NOT NULL default '0',
			`event_name` varchar(100) NOT NULL default '',
			`short_description` text NOT NULL,
			`registration_start` datetime NOT NULL default '0000-00-00 00:00:00',
			`registration_end` datetime NOT NULL default '0000-00-00 00:00:00',
			`listing_status` enum('Published','Unpublished') NOT NULL default 'Unpublished',
			`event_start` datetime NOT NULL default '0000-00-00 00:00:00',
			`event_end` datetime NOT NULL default '0000-00-00 00:00:00',
			`registration_limit` int(11) NOT NULL default '0',
			`address1` varchar(100) NOT NULL default '',
			`address2` varchar(100) NOT NULL default '',
			`city` varchar(50) NOT NULL default '',
			`state` char(2) NOT NULL default '',
			`zip` varchar(10) NOT NULL default '',
			`phone` varchar(10) NOT NULL default '',
			`login_required` ENUM( 'Y', 'N' ) NOT NULL DEFAULT 'N',
			`last_update_ts` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY  (`id`)
			) TYPE=MyISAM ;";

		dbDelta($t1);

		$t2 = "CREATE TABLE `".$em_registration_table."` (
  			`id` int(11) NOT NULL auto_increment,
  			`event_id` int(11) NOT NULL default '0',
  			`email_address` tinytext NOT NULL,
  			`first_name` varchar(100) NOT NULL default '',
  			`last_name` varchar(100) NOT NULL default '',
  			`address1` varchar(100) NOT NULL default '',
  			`address2` varchar(100) NOT NULL default '',
  			`city` varchar(64) NOT NULL default '',
  			`state` char(2) NOT NULL default '',
  			`zip` varchar(10) NOT NULL default '',
  			`phone` varchar(20) NOT NULL default '',
  			`last_update_ts` datetime NOT NULL default '0000-00-00 00:00:00',
  			PRIMARY KEY  (`id`),
  			KEY `I_user_event_rel_0` (`event_id`)
			) TYPE=MyISAM ;";

		dbDelta($t2);

		$t3 = "CREATE TABLE `".$em_answer_table."` (
			`registration_id` int(11) NOT NULL default '0',
			`question_id` int(11) NOT NULL default '0',
			`answer` text NOT NULL,
			PRIMARY KEY  (`registration_id`,`question_id`)
			) TYPE=MyISAM;";

		dbDelta($t3);

		$t4 = "CREATE TABLE `".$em_question_table."` (
			`id` int(11) NOT NULL auto_increment,
			`event_id` int(11) NOT NULL default '0',
			`sequence` int(11) NOT NULL default '0',
			`question_type` enum('TEXT','TEXTAREA','MULTIPLE','SINGLE') NOT NULL default 'TEXT',
			`question` tinytext NOT NULL,
			`values` tinytext NOT NULL,
			`required` ENUM( 'Y', 'N' ) NOT NULL DEFAULT 'N',
			PRIMARY KEY  (`id`)
			) TYPE=MyISAM ;";

		dbDelta($t4);
}

add_action("activate_EventManager/EventManager.php", "em_install");
